home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / cenviw1.zip / HEXDUMP.CMM < prev    next >
Text File  |  1993-08-09  |  3KB  |  104 lines

  1. /**********************************************************************
  2.  *** HexDump.bat - Hexidecimal dump of the contents of a file. This ***
  3.  ***               examples program uses the CEnvi file routines.   ***
  4.  ***               It calls upon NotePad to display the results of  ***
  5.  ***               the dump.                                        ***
  6.  **********************************************************************/
  7.  
  8. #include <MsgBox.lib>
  9. #include <PickFile.lib>
  10.  
  11. main(argc,argv)
  12. {
  13.    // prompt for file name if file wasn't passed in
  14.    if ( argc == 1 ) {
  15.       FileName = PromptForFile();
  16.    } else {
  17.       if ( argc != 2 )
  18.          Instructions();
  19.       FileName = argv[1];
  20.    }
  21.  
  22.    if ( NULL == FileName  ||  0 == FileName[0] )
  23.       Instructions();
  24.  
  25.    // get the name of the temporary destination file to write to
  26.    DumpFileName = tmpnam();
  27.  
  28.    // perform the dump operation. True for success else false
  29.    if ( FileDump(FileName,DumpFileName) ) {
  30.       // dump worked OK, so spawn notepad to view the result
  31.       spawn(P_WAIT,"NotePad.exe",DumpFileName);
  32.    }
  33.  
  34.    // remove the temporary file, whether it exists or not
  35.    remove(DumpFileName);
  36.  
  37.    return(EXIT_SUCCESS);
  38. }
  39.  
  40. FileDump(SrcFile,DstFile)
  41. {
  42.    success = FALSE;  // assume failure
  43.    srcFP = fopen(SrcFile,"rb");
  44.    if ( NULL == srcFP )
  45.       Error("Could not open file %s for reading.",SrcFile);
  46.    else {
  47.       dstFP = fopen(DstFile,"wt");
  48.       if ( NULL == dstFP )
  49.          Error("Could not open file %s for writing.");
  50.       else {
  51.          Unprintables = "\a\b\t\r\n\032\033"
  52.          success = TRUE;
  53.          for ( offset = 0; 0 < (count = fread(data,16,srcFP)); offset += 16 ) {
  54.             // display hex offset in file
  55.             fprintf(dstFP,"%06X  ",offset)
  56.             // display hex value for each number
  57.             for ( i = 0; i < count; i++ )
  58.                fprintf(dstFP,"%02X ",data[i])
  59.             // fill in any extra gaps if count < 16
  60.             while( i++ < 16 )
  61.                fprintf(dstFP,"   ")
  62.             // display ascii value for each printable character
  63.             // substitute a period for unprintable characters
  64.             data[count] = 0; // string must be null-terminated
  65.             while( (UnprintableIndex = strcspn(data,Unprintables)) < count )
  66.                data[UnprintableIndex] = '.';
  67.             fprintf(dstFP,"   %s\n",data)
  68.             if ( count < 16 )
  69.                break
  70.          }
  71.          fclose(dstFP);
  72.       }
  73.       fclose(srcFP);
  74.    }
  75.    return(success);
  76. }
  77.  
  78.  
  79. PromptForFile()
  80. {
  81.    return(GetOpenFileName(NULL,"HexDump File Selection"));
  82. }
  83.  
  84. Error(FormatString,arg1,arg2,arg3/*etc...*/)
  85. {
  86.    va_start(valist,FormatString);
  87.    vsprintf(message,FormatString,valist);
  88.    va_end(valist);
  89.    MessageBox(message);
  90. }
  91.  
  92.  
  93. Instructions()
  94. {
  95.    MessageBox(
  96.          "HexDump.cmm - This is a Cmm program to display a hexidecimal dump of\n"
  97.          "              any file.  Either execute with one argument, whcih is the\n"
  98.          "              name of the file to view, or with no arguments to be\n"
  99.          "              prompted for a file name."
  100.    );
  101.    exit(EXIT_FAILURE);
  102. }
  103.  
  104.